package com.zzy.demo.utils;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

import jodd.http.HttpBrowser;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;


/**
 * 
 * @author guokaige
 *
 */
public class HttpdemoTest {
	
	@Test
	public void  testApiGateWay(){
		HttpResponse response = HttpRequest
		        .get("http://localhost:5555/api-a-url/hello")
		        .query("name", "张三")
		        .query("accessToken", "kevin")
		        .queryEncoding("utf-8")
		        .send();
		System.out.println(response.bodyText());
	}
	
	@Test
	public void  testSmsServer(){
		
		Map<String, String>   templateParamsMap=new HashMap<String, String>();
		templateParamsMap.put("code", "4233");
		
		String templateParams=FastJsonUtil.fastJsonSerialize(templateParamsMap);
		
		//String encodeParams=DesEncryptUtil.encrypt(templateParams, "topnet@123456");
		String encodeParams=new Des().strEnc(templateParams, "topnet@123456", null, null);
		
		System.out.println("--------------"+encodeParams);

		HttpResponse response = HttpRequest
		        .get("http://localhost:8066/sendMsgOuter")
		        .query("appKey", "kevin")
		        .query("appSecret", "kevin")
		        .query("phoneNums", "18336465621")
		        .query("templateCode", "SMS_115385698")
		        .query("templateParams", encodeParams)
		        .queryEncoding("utf-8")
		        .send();
		System.out.println(response.bodyText());
	}
	
	
	@Test
	public void  testFeign(){
		
		Map<String, String>   templateParamsMap=new HashMap<String, String>();
		templateParamsMap.put("code", "4233");
		
		String templateParams=FastJsonUtil.fastJsonSerialize(templateParamsMap);
		
		//String encodeParams=DesEncryptUtil.encrypt(templateParams, "topnet@123456");
		String encodeParams=new Des().strEnc(templateParams, "topnet@123456", null, null);
		
		
		System.out.println("--------------"+encodeParams);

		HttpResponse response = HttpRequest
		        .get("http://localhost:9001/send")
		        .query("appKey", "kevin")
		        .query("appSecret", "kevin")
		        .query("phoneNums", "18336465621")
		        .query("templateCode", "SMS_115385698")
		        .query("templateParams", encodeParams)
		        .queryEncoding("utf-8")
		        .send();
		System.out.println(response.bodyText());
	}
	
	
	@Test
	public void  testApiGateWaySend(){
		
		Map<String, String>   templateParamsMap=new HashMap<String, String>();
		templateParamsMap.put("code", "4233");
		
		String templateParams=FastJsonUtil.fastJsonSerialize(templateParamsMap);
		
		//String encodeParams=DesEncryptUtil.encrypt(templateParams, "topnet@123456");
		String encodeParams=new Des().strEnc(templateParams, "topnet@123456", null, null);
		
		System.out.println("--------------"+encodeParams);

		HttpResponse response = HttpRequest
		        .get("http://localhost:5555/api-b/send")
		        .query("accessToken", "yue")
		        .query("appKey", "kevin")
		        .query("appSecret", "kevin")
		        .query("phoneNums", "18336465621")
		        .query("templateCode", "SMS_115385698")
		        .query("templateParams", encodeParams)
		        .queryEncoding("utf-8")
		        .send();
		System.out.println(response.bodyText());
	}
	
	
	@Test
	public void  postDemo(){
		//POST请求
		HttpResponse response = HttpRequest
		.post("http://localhost/g-main3/index.jspx")
		.form("userId", "10194")
		//这里与get的参数传递方式不同
		.form("userId2", "10194")
		.queryEncoding("utf-8")
		.send();
		System.out.println(response.bodyText());
	}
	
	
	@Test
	public void httpBrowserTests(){
		HttpBrowser browser = new HttpBrowser();

	    HttpRequest request = HttpRequest.get("http://192.168.3.35:9081/ECPS/index.jspx");
	    browser.sendRequest(request);

	    // process the page:
	    String page = browser.getPage();
	    
	    System.out.println(page);

	    // create new request
	    HttpRequest newRequest = HttpRequest.post("");

	    browser.sendRequest(newRequest);
	}

}
